﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;


namespace CARMS.Report
{

    [Function("Report.TestReportOne")]
    public partial class TestReportOne : PageBase
    {
        private int CurrentReportHeaderID
        {
            get
            {
                return GetIntByParameter("ID");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
            }

        } 


        private void LoadDefault()
        {

            var collection = new ReportOneCollection();
            collection.GetMulti(ReportOneFields.ReportHeaderId == CurrentReportHeaderID);
            if (collection.Count > 0)
            {
                txtContent.Text = collection[0].ReportOneName;

            }

        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            if (SaveReport(true))
            {
                RediretResolveUrl("~/Report/MyReportHistory.aspx");
            }
        }

        private bool SaveReport(bool boolSendMessage)
        {
            var reportCategory = new ReportCategoryEntity(ReportCategoryConst.TestReportOne);
            if (!reportCategory.IsNew)
            {
                var reportHeader = new ReportHeaderEntity(CurrentReportHeaderID);
                if (reportHeader.IsNew)
                {
                    reportHeader.Status = DictionaryCategoryConst.ReportStatus.Saved;
                }
                reportHeader.ReportCategoryId = ReportCategoryConst.TestReportOne;
                reportHeader.ApplyUserId = CurrentLoginUser.UserID;
                reportHeader.TargetUserId = reportCategory.ApproveUserId;
                reportHeader.CreationUrl = string.Empty;
                reportHeader.Save();

                reportHeader.CreationUrl = reportCategory.CreationUrl + "?ID=" + reportHeader.ReportHeaderId;
                reportHeader.ViewUrl = reportCategory.ViewUrl + "?ID=" + reportHeader.ReportHeaderId;
                reportHeader.Save();


                var reportChild = new ReportOneEntity();
                reportChild.ReportOneName = txtContent.Text.Trim();
                reportChild.ReportHeaderId = reportHeader.ReportHeaderId;
                reportChild.Save();

                if (boolSendMessage)
                {
                    reportHeader.Status = DictionaryCategoryConst.ReportStatus.UnderConfirm;
                    reportHeader.Save();
                    MessageBusiness.DeleteMessageByReportHeaderID(reportChild.ReportHeaderId, DictionaryCategoryConst.ButtonPermission.Add);
                    MessageBusiness.SendMessageByReportID(reportChild.ReportHeaderId,DictionaryCategoryConst.ButtonPermission.Add);

                }
                return true;
            }

            return false;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveReport(false))
            {
                Alert(GenerateAlert.SavSuccess);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            RediretResolveUrl(SiteUrls.Report.MyReportHistory);
        }
    }
}
